/*******************************************************************************
 * Copyright (c) 2014 Francesco Gabbrielli.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Francesco Gabbrielli - initial API and implementation
 ******************************************************************************/
package it.francescogabbrielli.android.solidalapp;

import it.francescogabbrielli.android.solidalapp.NetworkUtils.Status;

import java.util.HashSet;
import java.util.Hashtable;
import java.util.Set;

/**
 * 
 */
public class RetryManager {
	
	private SolidalApp app;
	
	private Set<RetryListener> retries;
	
	public RetryManager(SolidalApp app) {
		this.app = app;
		retries = new HashSet<RetryListener>();
	}
	
	synchronized void retry() {
		Set<RetryListener> temp = new HashSet<RetryListener>(retries);
		for (RetryListener l : temp) {
			retries.remove(l);
			l.retry();
		}
	}
		
	synchronized boolean add(RetryListener l) {
		if (app.getStatus().getNetworkStatus()==Status.CONNECTED) {
			retries.remove(l);
			l.retry();
			return true;
		} else {
			retries.add(l);
			return false;
		}
	}

	synchronized void remove(RetryListener l) {
		retries.remove(l);
	}
}
